package com.climber.android.commonsdk.glide

import android.content.Context
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool
import com.bumptech.glide.load.engine.cache.ExternalPreferredCacheDiskCacheFactory
import com.bumptech.glide.load.engine.cache.LruResourceCache
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator
import com.bumptech.glide.module.AppGlideModule
import com.bumptech.glide.request.RequestOptions

/**
 * Created by Mikes at 2019-09-05 15:08
 */
@GlideModule
class iQmiAppGlideModule : AppGlideModule() {
    val IMAGE_DISK_CACHE_MAX_SIZE = 100 * 1024 * 1024 //图片缓存文件最大值为100Mb

    override fun isManifestParsingEnabled(): Boolean = false

    override fun applyOptions(context: Context, builder: GlideBuilder) {
        val calculator = MemorySizeCalculator.Builder(context).build()
        val defaultMemoryCacheSize = calculator.getMemoryCacheSize()
        val defaultBitmapPoolSize = calculator.getBitmapPoolSize()
        val customMemoryCacheSize = (1.2 * defaultMemoryCacheSize)
        val customBitmapPoolSize = (1.2 * defaultBitmapPoolSize)
        builder.setMemoryCache(LruResourceCache(customMemoryCacheSize.toLong()))
        builder.setBitmapPool(LruBitmapPool(customBitmapPoolSize.toLong()))
        builder.setDiskCache(
                ExternalPreferredCacheDiskCacheFactory(context, "glide", IMAGE_DISK_CACHE_MAX_SIZE.toLong()))
        builder.setDefaultRequestOptions(RequestOptions().format(DecodeFormat.PREFER_RGB_565))
    }
}